{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Discrete Bayes Tree\n",
    "\n",
    "An example of building a Bayes net, then eliminating it into a Bayes tree. Mirrors the code in `testDiscreteBayesTree.cpp` ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "source": [
    "GTSAM Copyright 2010-2022, Georgia Tech Research Corporation,\n",
    "Atlanta, Georgia 30332-0415\n",
    "All Rights Reserved\n",
    "\n",
    "Authors: Frank Dellaert, et al. (see THANKS for the full author list)\n",
    "\n",
    "See LICENSE for the license information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/borglab/gtsam/blob/develop/python/gtsam/examples/DiscreteBayesTree.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "%pip install --quiet gtsam-develop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gtsam import DiscreteBayesTree, DiscreteBayesNet, DiscreteKeys, DiscreteFactorGraph, Ordering\n",
    "from gtsam.symbol_shorthand import S"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def P(*args):\n",
    "    \"\"\" Create a DiscreteKeys instances from a variable number of DiscreteKey pairs.\"\"\"\n",
    "    #TODO: We can make life easier by providing variable argument functions in C++ itself.\n",
    "    dks = DiscreteKeys()\n",
    "    for key in args:\n",
    "        dks.push_back(key)\n",
    "    return dks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import graphviz\n",
    "class show(graphviz.Source):\n",
    "    \"\"\" Display an object with a dot method as a graph.\"\"\"\n",
    "\n",
    "    def __init__(self, obj):\n",
    "        \"\"\"Construct from object with 'dot' method.\"\"\"\n",
    "        # This small class takes an object, calls its dot function, and uses the\n",
    "        # resulting string to initialize a graphviz.Source instance. This in turn\n",
    "        # has a _repr_mimebundle_ method, which then renders it in the notebook.\n",
    "        super().__init__(obj.dot())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Title: G Pages: 1 -->\n",
       "<svg width=\"582pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 582.00 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 578,-256 578,4 -4,4\"/>\n",
       "<!-- 8 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>8</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">8</text>\n",
       "</g>\n",
       "<!-- 0 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>0</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"104\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"104\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n",
       "</g>\n",
       "<!-- 8&#45;&gt;0 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>8&#45;&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M42.58,-74.83C53.61,-64.81 68.59,-51.19 80.99,-39.92\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"83.66,-42.22 88.7,-32.91 78.95,-37.04 83.66,-42.22\"/>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"28\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"28\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">1</text>\n",
       "</g>\n",
       "<!-- 8&#45;&gt;1 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>8&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M27.25,-71.7C27.36,-63.98 27.49,-54.71 27.61,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"31.11,-46.15 27.76,-36.1 24.11,-46.05 31.11,-46.15\"/>\n",
       "</g>\n",
       "<!-- 12 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>12</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"158\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"158\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">12</text>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;8 -->\n",
       "<g id=\"edge17\" class=\"edge\">\n",
       "<title>12&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M137.53,-150.06C115.71,-138.4 80.96,-119.83 56.25,-106.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"57.79,-103.49 47.33,-101.86 54.5,-109.66 57.79,-103.49\"/>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;0 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>12&#45;&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M151.7,-144.43C142.44,-120.09 125.07,-74.4 114.04,-45.4\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"117.25,-44 110.43,-35.9 110.71,-46.49 117.25,-44\"/>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;1 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>12&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M144.58,-146.34C121.81,-121.47 75.47,-70.85 48.58,-41.48\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"50.88,-38.81 41.54,-33.79 45.72,-43.53 50.88,-38.81\"/>\n",
       "</g>\n",
       "<!-- 9 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>9</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"251\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"251\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">9</text>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;9 -->\n",
       "<g id=\"edge19\" class=\"edge\">\n",
       "<title>12&#45;&gt;9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M175.49,-147.83C189.52,-137.27 209.42,-122.3 225.26,-110.37\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"227.7,-112.92 233.59,-104.11 223.49,-107.32 227.7,-112.92\"/>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"251\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"251\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;2 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>12&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M167.96,-145.16C179.24,-127.3 198.23,-97.42 215,-72 221.25,-62.53 228.23,-52.19 234.37,-43.18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"237.42,-44.93 240.18,-34.69 231.64,-40.97 237.42,-44.93\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"178\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"178\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n",
       "</g>\n",
       "<!-- 12&#45;&gt;3 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>12&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M160.41,-143.87C163.82,-119.67 170.08,-75.21 174.14,-46.39\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"177.65,-46.58 175.58,-36.19 170.72,-45.6 177.65,-46.58\"/>\n",
       "</g>\n",
       "<!-- 9&#45;&gt;2 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>9&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M251,-71.7C251,-63.98 251,-54.71 251,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"254.5,-46.1 251,-36.1 247.5,-46.1 254.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- 9&#45;&gt;3 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>9&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M236.23,-74.83C225.84,-64.87 211.76,-51.37 200.04,-40.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"202.39,-37.54 192.75,-33.14 197.55,-42.59 202.39,-37.54\"/>\n",
       "</g>\n",
       "<!-- 10 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>10</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"323\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"323\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">10</text>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>4</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"399\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"399\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n",
       "</g>\n",
       "<!-- 10&#45;&gt;4 -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>10&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M338.38,-74.83C349.2,-64.87 363.86,-51.37 376.05,-40.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"378.66,-42.49 383.64,-33.14 373.92,-37.34 378.66,-42.49\"/>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>5</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"323\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"323\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">5</text>\n",
       "</g>\n",
       "<!-- 10&#45;&gt;5 -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>10&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M323,-71.7C323,-63.98 323,-54.71 323,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"326.5,-46.1 323,-36.1 319.5,-46.1 326.5,-46.1\"/>\n",
       "</g>\n",
       "<!-- 13 -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>13</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"416\" cy=\"-162\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"416\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">13</text>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;10 -->\n",
       "<g id=\"edge21\" class=\"edge\">\n",
       "<title>13&#45;&gt;10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M398.51,-147.83C384.48,-137.27 364.58,-122.3 348.74,-110.37\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"350.51,-107.32 340.41,-104.11 346.3,-112.92 350.51,-107.32\"/>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;4 -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>13&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M413.95,-143.87C411.05,-119.67 405.73,-75.21 402.28,-46.39\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"405.72,-45.7 401.06,-36.19 398.77,-46.53 405.72,-45.7\"/>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;5 -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>13&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M406.04,-145.16C394.76,-127.3 375.77,-97.42 359,-72 352.75,-62.53 345.77,-52.19 339.63,-43.18\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"342.36,-40.97 333.82,-34.69 336.58,-44.93 342.36,-40.97\"/>\n",
       "</g>\n",
       "<!-- 11 -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>11</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"547\" cy=\"-90\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"547\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">11</text>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;11 -->\n",
       "<g id=\"edge23\" class=\"edge\">\n",
       "<title>13&#45;&gt;11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M436.47,-150.06C458.29,-138.4 493.04,-119.83 517.75,-106.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"519.5,-109.66 526.67,-101.86 516.21,-103.49 519.5,-109.66\"/>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node13\" class=\"node\">\n",
       "<title>6</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"546\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"546\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">6</text>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;6 -->\n",
       "<g id=\"edge14\" class=\"edge\">\n",
       "<title>13&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M429.42,-146.34C452.19,-121.47 498.53,-70.85 525.42,-41.48\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"528.28,-43.53 532.46,-33.79 523.12,-38.81 528.28,-43.53\"/>\n",
       "</g>\n",
       "<!-- 7 -->\n",
       "<g id=\"node14\" class=\"node\">\n",
       "<title>7</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"473\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"473\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">7</text>\n",
       "</g>\n",
       "<!-- 13&#45;&gt;7 -->\n",
       "<g id=\"edge16\" class=\"edge\">\n",
       "<title>13&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M422.65,-144.43C432.48,-119.96 450.96,-73.91 462.59,-44.94\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"465.92,-46.05 466.39,-35.46 459.42,-43.44 465.92,-46.05\"/>\n",
       "</g>\n",
       "<!-- 11&#45;&gt;6 -->\n",
       "<g id=\"edge13\" class=\"edge\">\n",
       "<title>11&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M546.75,-71.7C546.64,-63.98 546.51,-54.71 546.39,-46.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"549.89,-46.05 546.24,-36.1 542.89,-46.15 549.89,-46.05\"/>\n",
       "</g>\n",
       "<!-- 11&#45;&gt;7 -->\n",
       "<g id=\"edge15\" class=\"edge\">\n",
       "<title>11&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M532.02,-74.83C521.49,-64.87 507.22,-51.37 495.34,-40.14\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"497.62,-37.47 487.95,-33.14 492.81,-42.56 497.62,-37.47\"/>\n",
       "</g>\n",
       "<!-- 14 -->\n",
       "<g id=\"node15\" class=\"node\">\n",
       "<title>14</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"286\" cy=\"-234\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"286\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">14</text>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;8 -->\n",
       "<g id=\"edge18\" class=\"edge\">\n",
       "<title>14&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M260.11,-228.31C226.5,-221.42 166.85,-206.44 122,-180 92.06,-162.35 63.8,-133.78 46.01,-113.78\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"48.49,-111.3 39.28,-106.06 43.21,-115.9 48.49,-111.3\"/>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;12 -->\n",
       "<g id=\"edge25\" class=\"edge\">\n",
       "<title>14&#45;&gt;12</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M265.72,-221.91C244.56,-210.34 211.19,-192.09 187.2,-178.97\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"188.64,-175.77 178.19,-174.04 185.28,-181.91 188.64,-175.77\"/>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;9 -->\n",
       "<g id=\"edge20\" class=\"edge\">\n",
       "<title>14&#45;&gt;9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M281.85,-216.15C275.85,-191.81 264.7,-146.57 257.57,-117.67\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"260.94,-116.7 255.15,-107.82 254.14,-118.37 260.94,-116.7\"/>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;10 -->\n",
       "<g id=\"edge22\" class=\"edge\">\n",
       "<title>14&#45;&gt;10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M290.39,-216.15C296.73,-191.81 308.52,-146.57 316.05,-117.67\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"319.48,-118.38 318.62,-107.82 312.71,-116.62 319.48,-118.38\"/>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;13 -->\n",
       "<g id=\"edge26\" class=\"edge\">\n",
       "<title>14&#45;&gt;13</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M306.31,-222.06C327.84,-210.47 362.05,-192.05 386.54,-178.86\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"388.26,-181.91 395.4,-174.09 384.94,-175.75 388.26,-181.91\"/>\n",
       "</g>\n",
       "<!-- 14&#45;&gt;11 -->\n",
       "<g id=\"edge24\" class=\"edge\">\n",
       "<title>14&#45;&gt;11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M311.92,-228.43C345.88,-221.64 406.48,-206.73 452,-180 481.98,-162.4 510.22,-133.82 528,-113.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"530.81,-115.92 534.73,-106.08 525.52,-111.32 530.81,-115.92\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<__main__.show at 0x109c615b0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Define DiscreteKey pairs.\n",
    "keys = [(j, 2) for j in range(15)]\n",
    "\n",
    "# Create thin-tree Bayesnet.\n",
    "bayesNet = DiscreteBayesNet()\n",
    "\n",
    "\n",
    "bayesNet.add(keys[0], P(keys[8], keys[12]), \"2/3 1/4 3/2 4/1\")\n",
    "bayesNet.add(keys[1], P(keys[8], keys[12]), \"4/1 2/3 3/2 1/4\")\n",
    "bayesNet.add(keys[2], P(keys[9], keys[12]), \"1/4 8/2 2/3 4/1\")\n",
    "bayesNet.add(keys[3], P(keys[9], keys[12]), \"1/4 2/3 3/2 4/1\")\n",
    "\n",
    "bayesNet.add(keys[4], P(keys[10], keys[13]), \"2/3 1/4 3/2 4/1\")\n",
    "bayesNet.add(keys[5], P(keys[10], keys[13]), \"4/1 2/3 3/2 1/4\")\n",
    "bayesNet.add(keys[6], P(keys[11], keys[13]), \"1/4 3/2 2/3 4/1\")\n",
    "bayesNet.add(keys[7], P(keys[11], keys[13]), \"1/4 2/3 3/2 4/1\")\n",
    "\n",
    "bayesNet.add(keys[8], P(keys[12], keys[14]), \"T 1/4 3/2 4/1\")\n",
    "bayesNet.add(keys[9], P(keys[12], keys[14]), \"4/1 2/3 F 1/4\")\n",
    "bayesNet.add(keys[10], P(keys[13], keys[14]), \"1/4 3/2 2/3 4/1\")\n",
    "bayesNet.add(keys[11], P(keys[13], keys[14]), \"1/4 2/3 3/2 4/1\")\n",
    "\n",
    "bayesNet.add(keys[12], P(keys[14]), \"3/1 3/1\")\n",
    "bayesNet.add(keys[13], P(keys[14]), \"1/3 3/1\")\n",
    "\n",
    "bayesNet.add(keys[14], P(), \"1/3\")\n",
    "\n",
    "show(bayesNet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 0, 7: 1, 8: 0, 9: 0, 10: 0, 11: 0, 12: 1, 13: 1, 14: 0}\n",
      "DiscreteValues{0: 0, 1: 1, 2: 0, 3: 0, 4: 1, 5: 0, 6: 0, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n",
      "DiscreteValues{0: 1, 1: 0, 2: 1, 3: 1, 4: 0, 5: 0, 6: 1, 7: 0, 8: 1, 9: 0, 10: 1, 11: 1, 12: 0, 13: 1, 14: 0}\n",
      "DiscreteValues{0: 1, 1: 1, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 0, 11: 0, 12: 1, 13: 0, 14: 1}\n",
      "DiscreteValues{0: 0, 1: 0, 2: 1, 3: 0, 4: 1, 5: 1, 6: 1, 7: 0, 8: 1, 9: 1, 10: 0, 11: 1, 12: 0, 13: 0, 14: 1}\n"
     ]
    }
   ],
   "source": [
    "# Sample Bayes net (needs conditionals added in elimination order!)\n",
    "for i in range(5):\n",
    "    print(bayesNet.sample())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"360pt\" height=\"56pt\"\n",
       " viewBox=\"0.00 0.00 360.00 56.13\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(0.36 0.36) rotate(0) translate(4 151.6)\">\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-151.6 994,-151.6 994,4 -4,4\"/>\n",
       "<!-- var0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>var0</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"27\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n",
       "</g>\n",
       "<!-- factor0 -->\n",
       "<g id=\"node16\" class=\"node\">\n",
       "<title>factor0</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"99\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var0&#45;&#45;factor0 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>var0&#45;&#45;factor0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M41.57,-114.43C60.64,-95.9 92.31,-65.1 98.08,-59.49\"/>\n",
       "</g>\n",
       "<!-- var1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>var1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"171\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">1</text>\n",
       "</g>\n",
       "<!-- factor1 -->\n",
       "<g id=\"node17\" class=\"node\">\n",
       "<title>factor1</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"171\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var1&#45;&#45;factor1 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>var1&#45;&#45;factor1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M171,-111.3C171,-92.51 171,-64.5 171,-59.41\"/>\n",
       "</g>\n",
       "<!-- var2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>var2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"459\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"459\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n",
       "</g>\n",
       "<!-- factor2 -->\n",
       "<g id=\"node18\" class=\"node\">\n",
       "<title>factor2</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"402\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var2&#45;&#45;factor2 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>var2&#45;&#45;factor2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M446.63,-113.41C431.44,-94.75 407.13,-64.9 402.7,-59.46\"/>\n",
       "</g>\n",
       "<!-- var3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>var3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"315\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"315\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n",
       "</g>\n",
       "<!-- factor3 -->\n",
       "<g id=\"node19\" class=\"node\">\n",
       "<title>factor3</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"315\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var3&#45;&#45;factor3 -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>var3&#45;&#45;factor3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M315,-111.3C315,-92.51 315,-64.5 315,-59.41\"/>\n",
       "</g>\n",
       "<!-- var4 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>var4</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"531\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"531\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n",
       "</g>\n",
       "<!-- factor4 -->\n",
       "<g id=\"node20\" class=\"node\">\n",
       "<title>factor4</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"588\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var4&#45;&#45;factor4 -->\n",
       "<g id=\"edge13\" class=\"edge\">\n",
       "<title>var4&#45;&#45;factor4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M543.37,-113.41C558.56,-94.75 582.87,-64.9 587.3,-59.46\"/>\n",
       "</g>\n",
       "<!-- var5 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>var5</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"675\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"675\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">5</text>\n",
       "</g>\n",
       "<!-- factor5 -->\n",
       "<g id=\"node21\" class=\"node\">\n",
       "<title>factor5</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"675\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var5&#45;&#45;factor5 -->\n",
       "<g id=\"edge16\" class=\"edge\">\n",
       "<title>var5&#45;&#45;factor5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M675,-111.3C675,-92.51 675,-64.5 675,-59.41\"/>\n",
       "</g>\n",
       "<!-- var6 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>var6</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"819\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"819\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">6</text>\n",
       "</g>\n",
       "<!-- factor6 -->\n",
       "<g id=\"node22\" class=\"node\">\n",
       "<title>factor6</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"819\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var6&#45;&#45;factor6 -->\n",
       "<g id=\"edge19\" class=\"edge\">\n",
       "<title>var6&#45;&#45;factor6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M819,-111.3C819,-92.51 819,-64.5 819,-59.41\"/>\n",
       "</g>\n",
       "<!-- var7 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>var7</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"963\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"963\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">7</text>\n",
       "</g>\n",
       "<!-- factor7 -->\n",
       "<g id=\"node23\" class=\"node\">\n",
       "<title>factor7</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"891\" cy=\"-57.6\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var7&#45;&#45;factor7 -->\n",
       "<g id=\"edge22\" class=\"edge\">\n",
       "<title>var7&#45;&#45;factor7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M948.43,-114.43C929.36,-95.9 897.69,-65.1 891.92,-59.49\"/>\n",
       "</g>\n",
       "<!-- var8 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>var8</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"99\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">8</text>\n",
       "</g>\n",
       "<!-- var8&#45;&#45;factor0 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>var8&#45;&#45;factor0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99,-111.3C99,-92.51 99,-64.5 99,-59.41\"/>\n",
       "</g>\n",
       "<!-- var8&#45;&#45;factor1 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>var8&#45;&#45;factor1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M113.57,-114.43C132.64,-95.9 164.31,-65.1 170.08,-59.49\"/>\n",
       "</g>\n",
       "<!-- factor8 -->\n",
       "<g id=\"node24\" class=\"node\">\n",
       "<title>factor8</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"243\" cy=\"-1.8\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var8&#45;&#45;factor8 -->\n",
       "<g id=\"edge25\" class=\"edge\">\n",
       "<title>var8&#45;&#45;factor8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M106.74,-112.11C116.65,-92.53 135.51,-59.96 160,-39.6 188.12,-16.22 233.44,-4.99 241.69,-3.09\"/>\n",
       "</g>\n",
       "<!-- var9 -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>var9</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"387\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"387\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">9</text>\n",
       "</g>\n",
       "<!-- var9&#45;&#45;factor2 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>var9&#45;&#45;factor2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M390.63,-111.65C394.65,-92.88 400.72,-64.56 401.83,-59.42\"/>\n",
       "</g>\n",
       "<!-- var9&#45;&#45;factor3 -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>var9&#45;&#45;factor3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M372.43,-114.43C353.36,-95.9 321.69,-65.1 315.92,-59.49\"/>\n",
       "</g>\n",
       "<!-- factor9 -->\n",
       "<g id=\"node25\" class=\"node\">\n",
       "<title>factor9</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"372\" cy=\"-1.8\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var9&#45;&#45;factor9 -->\n",
       "<g id=\"edge28\" class=\"edge\">\n",
       "<title>var9&#45;&#45;factor9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M384.98,-111.63C381.14,-79.44 373.09,-11.96 372.1,-3.65\"/>\n",
       "</g>\n",
       "<!-- var10 -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>var10</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"603\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"603\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">10</text>\n",
       "</g>\n",
       "<!-- var10&#45;&#45;factor4 -->\n",
       "<g id=\"edge14\" class=\"edge\">\n",
       "<title>var10&#45;&#45;factor4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M599.37,-111.65C595.35,-92.88 589.28,-64.56 588.17,-59.42\"/>\n",
       "</g>\n",
       "<!-- var10&#45;&#45;factor5 -->\n",
       "<g id=\"edge17\" class=\"edge\">\n",
       "<title>var10&#45;&#45;factor5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M617.57,-114.43C636.64,-95.9 668.31,-65.1 674.08,-59.49\"/>\n",
       "</g>\n",
       "<!-- factor10 -->\n",
       "<g id=\"node26\" class=\"node\">\n",
       "<title>factor10</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"618\" cy=\"-1.8\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var10&#45;&#45;factor10 -->\n",
       "<g id=\"edge31\" class=\"edge\">\n",
       "<title>var10&#45;&#45;factor10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M605.02,-111.63C608.86,-79.44 616.91,-11.96 617.9,-3.65\"/>\n",
       "</g>\n",
       "<!-- var11 -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>var11</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"891\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"891\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">11</text>\n",
       "</g>\n",
       "<!-- var11&#45;&#45;factor6 -->\n",
       "<g id=\"edge20\" class=\"edge\">\n",
       "<title>var11&#45;&#45;factor6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M876.43,-114.43C857.36,-95.9 825.69,-65.1 819.92,-59.49\"/>\n",
       "</g>\n",
       "<!-- var11&#45;&#45;factor7 -->\n",
       "<g id=\"edge23\" class=\"edge\">\n",
       "<title>var11&#45;&#45;factor7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M891,-111.3C891,-92.51 891,-64.5 891,-59.41\"/>\n",
       "</g>\n",
       "<!-- factor11 -->\n",
       "<g id=\"node27\" class=\"node\">\n",
       "<title>factor11</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"747\" cy=\"-1.8\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var11&#45;&#45;factor11 -->\n",
       "<g id=\"edge34\" class=\"edge\">\n",
       "<title>var11&#45;&#45;factor11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M883.26,-112.11C873.35,-92.53 854.49,-59.96 830,-39.6 801.88,-16.22 756.56,-4.99 748.31,-3.09\"/>\n",
       "</g>\n",
       "<!-- var12 -->\n",
       "<g id=\"node13\" class=\"node\">\n",
       "<title>var12</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"243\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"243\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">12</text>\n",
       "</g>\n",
       "<!-- var14 -->\n",
       "<g id=\"node15\" class=\"node\">\n",
       "<title>var14</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"495\" cy=\"-57.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"495\" y=\"-53.9\" font-family=\"Times,serif\" font-size=\"14.00\">14</text>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;var14 -->\n",
       "<g id=\"edge37\" class=\"edge\">\n",
       "<title>var12&#45;&#45;var14</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M263.64,-117.99C268.59,-115.68 273.92,-113.4 279,-111.6 314.39,-99.05 420.69,-74.97 469.72,-64.13\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor0 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M221.75,-118.27C184.51,-100.17 110.42,-64.15 100.18,-59.18\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor1 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M228.43,-114.43C209.36,-95.9 177.69,-65.1 171.92,-59.49\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor2 -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M264.26,-118.41C269.09,-116.14 274.21,-113.76 279,-111.6 329.02,-89.02 391.35,-63.03 400.78,-59.11\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor3 -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M257.57,-114.43C276.64,-95.9 308.31,-65.1 314.08,-59.49\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor8 -->\n",
       "<g id=\"edge26\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M243,-111.37C243,-79.07 243,-11.91 243,-3.64\"/>\n",
       "</g>\n",
       "<!-- var12&#45;&#45;factor9 -->\n",
       "<g id=\"edge29\" class=\"edge\">\n",
       "<title>var12&#45;&#45;factor9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M251.2,-112.09C261.42,-92.78 280.47,-60.78 304,-39.6 326.75,-19.12 362.91,-5.93 370.55,-3.29\"/>\n",
       "</g>\n",
       "<!-- var13 -->\n",
       "<g id=\"node14\" class=\"node\">\n",
       "<title>var13</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"747\" cy=\"-129.6\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"747\" y=\"-125.9\" font-family=\"Times,serif\" font-size=\"14.00\">13</text>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;var14 -->\n",
       "<g id=\"edge38\" class=\"edge\">\n",
       "<title>var13&#45;&#45;var14</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M726.36,-117.99C721.41,-115.68 716.08,-113.4 711,-111.6 675.61,-99.05 569.31,-74.97 520.28,-64.13\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor4 -->\n",
       "<g id=\"edge15\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M725.74,-118.41C720.91,-116.14 715.79,-113.76 711,-111.6 660.98,-89.02 598.65,-63.03 589.22,-59.11\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor5 -->\n",
       "<g id=\"edge18\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M732.43,-114.43C713.36,-95.9 681.69,-65.1 675.92,-59.49\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor6 -->\n",
       "<g id=\"edge21\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M761.57,-114.43C780.64,-95.9 812.31,-65.1 818.08,-59.49\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor7 -->\n",
       "<g id=\"edge24\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M768.25,-118.27C805.49,-100.17 879.58,-64.15 889.82,-59.18\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor10 -->\n",
       "<g id=\"edge32\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M738.8,-112.09C728.58,-92.78 709.53,-60.78 686,-39.6 663.25,-19.12 627.09,-5.93 619.45,-3.29\"/>\n",
       "</g>\n",
       "<!-- var13&#45;&#45;factor11 -->\n",
       "<g id=\"edge35\" class=\"edge\">\n",
       "<title>var13&#45;&#45;factor11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M747,-111.37C747,-79.07 747,-11.91 747,-3.64\"/>\n",
       "</g>\n",
       "<!-- var14&#45;&#45;factor8 -->\n",
       "<g id=\"edge27\" class=\"edge\">\n",
       "<title>var14&#45;&#45;factor8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M469.45,-51.15C408.89,-38.22 260.47,-6.53 244.41,-3.1\"/>\n",
       "</g>\n",
       "<!-- var14&#45;&#45;factor9 -->\n",
       "<g id=\"edge30\" class=\"edge\">\n",
       "<title>var14&#45;&#45;factor9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M473.01,-46.98C440.39,-32.72 382.07,-7.2 373.16,-3.31\"/>\n",
       "</g>\n",
       "<!-- var14&#45;&#45;factor10 -->\n",
       "<g id=\"edge33\" class=\"edge\">\n",
       "<title>var14&#45;&#45;factor10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M516.99,-46.98C549.61,-32.72 607.93,-7.2 616.84,-3.31\"/>\n",
       "</g>\n",
       "<!-- var14&#45;&#45;factor11 -->\n",
       "<g id=\"edge36\" class=\"edge\">\n",
       "<title>var14&#45;&#45;factor11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M520.55,-51.15C581.11,-38.22 729.53,-6.53 745.59,-3.1\"/>\n",
       "</g>\n",
       "<!-- factor14 -->\n",
       "<g id=\"node28\" class=\"node\">\n",
       "<title>factor14</title>\n",
       "<ellipse fill=\"black\" stroke=\"black\" cx=\"495\" cy=\"-1.8\" rx=\"1.8\" ry=\"1.8\"/>\n",
       "</g>\n",
       "<!-- var14&#45;&#45;factor14 -->\n",
       "<g id=\"edge39\" class=\"edge\">\n",
       "<title>var14&#45;&#45;factor14</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M495,-39.58C495,-25.79 495,-7.97 495,-3.73\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<__main__.show at 0x109c61f10>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a factor graph out of the Bayes net.\n",
    "factorGraph = DiscreteFactorGraph(bayesNet)\n",
    "show(factorGraph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.50.0 (0)\n",
       " -->\n",
       "<!-- Title: G Pages: 1 -->\n",
       "<svg width=\"645pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 644.59 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-256 640.59,-256 640.59,4 -4,4\"/>\n",
       "<!-- 0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>0</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"182.35\" cy=\"-234\" rx=\"38.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"182.35\" y=\"-230.3\" font-family=\"Times,serif\" font-size=\"14.00\">8,12,14</text>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"38.35\" cy=\"-162\" rx=\"38.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.35\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">0 : 8,12</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>0&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M156.6,-220.49C133.4,-209.21 99.09,-192.53 73.39,-180.04\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"74.88,-176.87 64.35,-175.64 71.82,-183.16 74.88,-176.87\"/>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"133.35\" cy=\"-162\" rx=\"38.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"133.35\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">1 : 8,12</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;2 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>0&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M170.98,-216.76C165.01,-208.23 157.55,-197.58 150.86,-188.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"153.59,-185.82 144.99,-179.63 147.86,-189.83 153.59,-185.82\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"232.35\" cy=\"-162\" rx=\"42.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"232.35\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">9 : 12,14</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>0&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M193.94,-216.76C200.04,-208.23 207.65,-197.58 214.48,-188.02\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"217.5,-189.81 220.47,-179.63 211.81,-185.74 217.5,-189.81\"/>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>6</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"388.35\" cy=\"-162\" rx=\"47.39\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"388.35\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">10,13 : 14</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;6 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>0&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M212.74,-222.67C247.51,-210.86 304.74,-191.41 344.37,-177.94\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"345.7,-181.19 354.04,-174.66 343.45,-174.56 345.7,-181.19\"/>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>4</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"137.35\" cy=\"-90\" rx=\"38.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"137.35\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">2 : 9,12</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;4 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>3&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M211.73,-145.81C198.23,-135.86 180.41,-122.73 165.59,-111.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"167.4,-108.79 157.27,-105.68 163.24,-114.43 167.4,-108.79\"/>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>5</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"232.35\" cy=\"-90\" rx=\"38.19\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"232.35\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">3 : 9,12</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;5 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>3&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M232.35,-143.7C232.35,-135.98 232.35,-126.71 232.35,-118.11\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"235.85,-118.1 232.35,-108.1 228.85,-118.1 235.85,-118.1\"/>\n",
       "</g>\n",
       "<!-- 7 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>7</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"331.35\" cy=\"-90\" rx=\"42.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"331.35\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">4 : 10,13</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;7 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>6&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M374.84,-144.41C367.81,-135.78 359.08,-125.06 351.29,-115.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"353.79,-113.01 344.76,-107.47 348.36,-117.43 353.79,-113.01\"/>\n",
       "</g>\n",
       "<!-- 8 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>8</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"435.35\" cy=\"-90\" rx=\"42.79\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"435.35\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">5 : 10,13</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;8 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>6&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M399.48,-144.41C405.22,-135.87 412.33,-125.28 418.7,-115.79\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"421.62,-117.72 424.29,-107.47 415.81,-113.82 421.62,-117.72\"/>\n",
       "</g>\n",
       "<!-- 9 -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>9</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"543.35\" cy=\"-90\" rx=\"46.59\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"543.35\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">11 : 13,14</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;9 -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>6&#45;&gt;9</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M417.87,-147.67C442.62,-136.49 478.13,-120.45 505.06,-108.29\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"506.53,-111.47 514.2,-104.16 503.65,-105.09 506.53,-111.47\"/>\n",
       "</g>\n",
       "<!-- 10 -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>10</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"492.35\" cy=\"-18\" rx=\"42.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"492.35\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">6 : 11,13</text>\n",
       "</g>\n",
       "<!-- 9&#45;&gt;10 -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>9&#45;&gt;10</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M531.26,-72.41C525.04,-63.87 517.32,-53.28 510.41,-43.79\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"513.06,-41.49 504.35,-35.47 507.41,-45.61 513.06,-41.49\"/>\n",
       "</g>\n",
       "<!-- 11 -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>11</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"594.35\" cy=\"-18\" rx=\"42.49\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"594.35\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">7 : 11,13</text>\n",
       "</g>\n",
       "<!-- 9&#45;&gt;11 -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>9&#45;&gt;11</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M555.43,-72.41C561.66,-63.87 569.37,-53.28 576.29,-43.79\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"579.29,-45.61 582.35,-35.47 573.63,-41.49 579.29,-45.61\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<__main__.show at 0x109c61b50>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a BayesTree out of the factor graph.\n",
    "ordering = Ordering()\n",
    "for j in range(15): ordering.push_back(j)\n",
    "bayesTree = factorGraph.eliminateMultifrontal(ordering)\n",
    "show(bayesTree)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3.8.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
